enyo.kind({
	name: "AssessmentUI_CreateSummary",
	kind: "AssessmentUI_PanelBase",
	
	caption: "Assessment Summary",
	submitLeftCaption: "Run",
	
	components: [
		{classes: "panel-body", components: [
			
			{kind: "hpit.Divider", caption:"Request Type", collapsable: true, isExpanded: true, components: [
				{name: "typeContent", content : ""}, 
			]},
			{kind: "hpit.Divider", caption:"Customer Country", collapsable: true, isExpanded: true, components: [
				{name: "countryContent", content : ""}, 
			]},
			{kind: "hpit.Divider", caption:"Customer City", collapsable: true, isExpanded: true, components: [
				{name: "cityContent", content : ""}, 
			]},
			{name:"Languages", kind: "hpit.Divider", caption:"Languages", collapsable: true, isExpanded: true, components: [
			]},
			{name:"Devices", kind: "hpit.Divider", caption:"Devices", collapsable: true, isExpanded: true, components: [
			]},
			{kind : "hpit.Divider", caption : "Notification Recipients", collapsable: true, isExpanded: true, components : [
				{name:"emailRepeater", kind:"Repeater",onSetupItem:"emailRepeaterSetupRow", components:[
					{style:"display: table; margin-top: 10px;", components:[
						{kind:"Image",src: "images/btn_del.png", ontap: "deleteButtonClicked", style: "float: left; margin-right: 10px"},
						{name:"emailCaption", content:"", style:"float:right;"}
					]}
				]},
				{kind: "onyx.Groupbox", classes: "groupbox", components: [
					{kind:"onyx.GroupboxHeader", content:"Add notification Recipients", classes: "group-header"},
					{name:"emailInput", kind: "onyx.InputDecorator", components: [
						{name:"newEmailInput", kind: "onyx.Input", padding:"10px, 0px, 10px, 0px;",style:"width:95%;", placeholder: "Click here to add...", components:[
							{kind:"onyx.Button", content:"Add", ontap:"addButtonClicked"}
						]}
					]},
				]}
			]},
			{kind : "hpit.Divider", caption : "Subscription", collapsable: true, isExpanded: true, components : [
				{classes:"clearfix", components:[
					{style:"float:left;", content: "A Subscription is a Saved Analysis Request which can be scheduled or manually executed later"},
					{name:"ToggleButton", kind: "onyx.ToggleButton", style:"float:right;", value: false, onContent: "YES", offContent: "NO", onChange: "subscriptionToggled"}
				]},
				{name: "subscriptionSettings", showing: false,  components: [
					{kind: "onyx.Groupbox", classes: "groupbox", components: [
						{kind:"onyx.GroupboxHeader", content:"Sybscription Title", classes: "group-header"},
						{kind: "onyx.InputDecorator", components: [
							{name: "titleInput", kind: "onyx.Input", padding:"10px, 0px, 10px, 0px;",style:"width:95%;", placeholder: "Input title of the subscription...."}
						]}
					]},
					{kind: "onyx.Groupbox", classes: "groupbox", components: [
						{kind:"onyx.GroupboxHeader", content:"Frequency", classes: "group-header"},
						{name: "frequencyCodePicker", kind: "onyx.PickerDecorator", components: [
							{style:"width:100%; background-color:#1E1E1E; color: #fff; font-weight: bold;"},
							{name:"frequencyCodePicker", kind: "onyx.Picker", style:"width:100%,  background-color:#1E1E1E",onSelect:"frequencyCodePickerChanged", components: [
								{content: "Unscheduled", active: true},
								{content: "Monthly"},
								{content: "Weekly"},
								{content: "Daily"}
							]}
						]},
						{name: "frequencyNumberPicker", kind: "onyx.PickerDecorator", style:"margin-top: 10px;",showing: false, components: [
							{style:"width:100%; background-color:#1E1E1E; color: #fff; font-weight: bold;"},
							{name:"frequencyNumber", kind: "onyx.Picker", onSelect:"frequencyNumberPickerChanged", components: [
							]}
						]},
					]}
				]}
			]},
		]},
	],
	
	create: function() {
		this.inherited(arguments);
		AssessmentUI_Data.current_page = this;
		this.initPanel();
	},
	
	initPanel: function() {
		AssessmentUI_Data.notificationRecipients = [];
		AssessmentUI_Data.notificationRecipients.push(AssessmentUI_Data.current_user);
		AssessmentUI_Data.subScriptionTitle = null;
		AssessmentUI_Data.frequencyCode = 1;
		AssessmentUI_Data.frequencyNum = 1;
		AssessmentUI_Data.runImmediality = false;
		AssessmentUI_Data.saveSubscription = false;
		AssessmentUI_Data.newRequestID = -1;
		
		this.$.emailRepeater.count = 1;
		this.$.emailRepeater.build();
	
		//Show Type
		this.$.typeContent.setContent(AssessmentUI_Data.selected_AnalysisTypePayload.defaultAnalysisTypeName);
		
		//Show Country
		this.$.countryContent.setContent(AssessmentUI_Data.selected_CountryName);
		
		//Show City
		this.$.cityContent.setContent(AssessmentUI_Data.selected_CityName);

		//Show language
		var languageList = "";
		var languageControl = this.$.Languages;
		languageControl.destroyClientControls();
		
		for(var i = 0; i < AssessmentUI_Data.selected_AnalysisTypePayload.supportedLocales.length; i++) {
			var local = AssessmentUI_Data.selected_AnalysisTypePayload.supportedLocales[i];
			if(local.checked) {
				languageList = {content: local.displayedNameInEnglish +";"};
				languageControl.createComponent(languageList, {owner: languageControl.owner});
				languageControl.render();
			}
		}
		
		//Show Devices
		var deviceList = "";
		var deviceControl = this.$.Devices;
		deviceControl.destroyClientControls();
		for(var i = 0; i < AssessmentUI_Data.payload_Devices.length; i++) {
			if(AssessmentUI_Data.payload_Devices.payloadChecked[i]) {
				deviceList = {content: AssessmentUI_Data.payload_Devices[i].hostname + ";"};
				deviceControl.createComponent(deviceList, {owner: deviceControl.owner});
				deviceControl.render();
			}
		}
	},
	
	addButtonClicked: function() {
		var strEmail = this.$.newEmailInput.getValue();
		if(this.checkMail(strEmail)) {
			AssessmentUI_Data.notificationRecipients.push(strEmail);
			this.$.newEmailInput.setValue("");
			this.$.emailRepeater.count += 1;
			this.$.emailRepeater.build();
		}
		else {
			AssessmentUI_Data.parentWnd.showMsg("Email address not valid.");
			this.$.emailInput.receiveFocus();
		}
	},
	
	checkMail: function(strEmail) {
		var reg = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
		return reg.test(strEmail);
	},
	
	subscriptionToggled: function() {
		AssessmentUI_Data.saveSubscription = this.$.ToggleButton.value;
		this.$.subscriptionSettings.setShowing(AssessmentUI_Data.saveSubscription);
		var submitLeftContent = AssessmentUI_Data.saveSubscription ? "Save & Run" : "Run";
		var submitRightContent = AssessmentUI_Data.saveSubscription ? "Save" : "";
		AssessmentUI_Data.parentWnd.$.footer.setSubmitLeftCaption(submitLeftContent);
		AssessmentUI_Data.parentWnd.$.footer.setSubmitRightCaption(submitRightContent);
	},
	
	frequencyCodePickerChanged: function(inSender, inEvent) {
		AssessmentUI_Data.frequencyCode = inEvent.content;
		var frequencyNumberControl = this.$.frequencyNumber;
		var frequencyNumber = null;
		frequencyNumberControl.destroyClientControls();
		
		switch(AssessmentUI_Data.frequencyCode) {
			case "Unscheduled":
				this.$.frequencyNumberPicker.setShowing(false);
				break;
			case "Monthly":
				this.$.frequencyNumberPicker.setShowing(true);
				frequencyNumber = {content: "January", value: 1, active: true};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "February", value: 2};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "March", value: 3};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "April", value: 4};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "May", value: 5};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "June", value: 6};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "July", value: 7};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "August", value: 8};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "September", value: 9};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "October", value: 10};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "November", value: 11};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "December", value: 12};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				this.$.frequencyNumberPicker.render();				
				break;
			case "Weekly":
				this.$.frequencyNumberPicker.setShowing(true);
				frequencyNumber = {content: "Monday", value: 1, active: true};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "Tuesday", value: 2};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "Wednesday", value: 3};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "Thursday", value: 4};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "Friday", value: 5};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "Saturday", value: 6};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				frequencyNumber = {content: "Sunday", value: 7};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				this.$.frequencyNumberPicker.render();
				break;
			case "Daily":
				this.$.frequencyNumberPicker.setShowing(true);
				frequencyNumber = {content: "1", value: 1, active: true};
				frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				
				for(var i = 2; i <= 28; i++){
					frequencyNumber = {content: i, value: i};
					frequencyNumberControl.createComponent(frequencyNumber, {owner: frequencyNumberControl.owner});
				}
				this.$.frequencyNumberPicker.render();
				break;
		}
		
	},
	
	frequencyNumberPickerChanged: function(inSender, inEvent) {
		AssessmentUI_Data.frequencyNum = inEvent.selected.value;
	},
	
	emailRepeaterSetupRow: function(inSender, inEvent) {
		var inIndex = inEvent.index;
		
		if(inIndex < AssessmentUI_Data.notificationRecipients.length) {
			var rowControl = inEvent.item;
			rowControl.$.emailCaption.setContent(AssessmentUI_Data.notificationRecipients[inIndex]);
		}		
		this.$.emailRepeater.render();
		return true;
	},
	
	deleteButtonClicked: function(inSender, inEvent) {
		var inIndex = inEvent.index;
		this.$.emailRepeater.count -= 1;
		AssessmentUI_Data.notificationRecipients.splice(inIndex, 1);
		this.$.emailRepeater.build();		
	},
});